/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.entity;

import java.util.ArrayList;
import java.util.TimerTask;
import module.Analytic;
import module.GameSetting;
import module.GameTurn;
import module.GenerateProject;
import module.GlobalConstant;
import module.ManageBid;
import module.ManageEmployee;
import wargame.dao.CompanyDAO;
import wargame.dao.EconomicDAO;
import wargame.dao.EmployeeDAO;
import wargame.dao.PlayerDAO;
import wargame.dao.NotificationDAO;
import wargame.dao.ProjectInjectDAO;

/**
 *
 * @author stanley.2007
 */
public class TurnTask extends TimerTask {

    public void run() {

        // is the times up yet?
        if (GameTurn.elapsed_second < GameTurn.turn_length && !GameTurn.game_pause) {

            // no, keeps ticking
            GameTurn.elapsed_second++;
            System.out.println("turn: " + GameTurn.current_turn + " timeElapsed: "
                    + GameTurn.elapsed_second + " turn length " + GameTurn.turn_length);

        } else if (GameTurn.elapsed_second >= GameTurn.turn_length && !GameTurn.game_pause) {

            // times up!
            // do batch  processing

            //remove all the injects for the turn
            ProjectInjectDAO.deleteCurrentTurn(GameTurn.current_turn);

            GameTurn.current_turn++;
            GameTurn.game_pause = true;

            // add new economic index
            Economic economicIndex = new Economic();
            economicIndex.setEconomic_index(GameTurn.index);
            economicIndex.setTurn(GameTurn.current_turn);
            EconomicDAO.insertEconomic(economicIndex);

            //process bid
            ManageBid.awardProjects();

            // Get all the analytic of the player's employees.
            //Must be done before restoring hours
            Analytic.createEmployeeUtilisationAnalytic();

            //restore employee hours
            EmployeeDAO.restore_employee_hours();

            //pay rental + employee salary
            ArrayList<Player> player_list = PlayerDAO.retrieveAllPlayers();
            for (Player player : player_list) {
                ArrayList<Employee> player_employees = EmployeeDAO.retrievePlayerEmployees(player);
                for (Employee employee : player_employees) {
                    boolean leaving = ManageEmployee.isEmployeeLeaving(employee, player);
                    //update player's employee record by deleting employee
                    if (leaving) {
                        Notification n = new Notification();
                        n.setPlayer_id(player.getPlayer_id());
                        n.setMessage("Your employee #" + employee.getEmployee_id() + " has left due to your poor management!");
                        n.setTurn(GameTurn.current_turn);
                        NotificationDAO.insertNotification(n);
                        EmployeeDAO.deleteEmployee(employee);
                    }
                }

                // pay rental
                int rental_cost = GameSetting.deductRental(player);

                //pay ALL employees salary
                int total_employee_salary = ManageEmployee.payEmployeesSalary(player);

                int player_balance = player.getBalance();
                player_balance -= (total_employee_salary + rental_cost);
                player.setBalance(player_balance);
                PlayerDAO.updatePlayer(player);
            }

            //generate new employees
            ManageEmployee.autoGenEmployee();

            //generate project
            ArrayList<Company> companyList = CompanyDAO.retrieveAll();
            for (Company company : companyList) {
                GenerateProject.createProject(company, GameTurn.current_turn);
            }

            // proceed to next turn
            GameTurn.turn_length = GlobalConstant.FIVE_MIN;
            GameTurn.elapsed_second = 0;
            GameTurn.game_pause = false;

            System.out.println("turn: " + GameTurn.current_turn + "timeElapsed: "
                    + GameTurn.elapsed_second + " turn length " + GameTurn.turn_length);

        }
    }
}
